﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;

namespace TheTVDB
{
    public class Episode
    {
        public int ID { get; set; }
        public int Season { get; set; }
        public int EpisodeNumber { get; set; }
        public int DVDChapter { get; set; }
        public int DVDDiscID { get; set; }
        public int DVDEpisode { get; set; }
        public int DVDSeason { get; set; }
        public string Director { get; set; }
        public string Name { get; set; }
        public DateTime FirstAired { get; set; }
        public List<string> GuestStars { get; set; }
        public string ImdbID { get; set; }
        public string Language { get; set; }
        public string Overview { get; set; }
        public string ProductionCode { get; set; }
        public float Rating { get; set; }
        public List<string> Writers { get; set; }
        public string FileName { get; set; }
        public long LastUpdated { get; set; }
        public int SeriesID { get; set; }
        public int SeasonID { get; set; }

        public Episode()
        {
            GuestStars = new List<string>();
            Writers = new List<string>();
        }

        public static Episode FromXml(XmlElement episodeNode)
        {
            Episode s = new Episode();
            foreach (XmlElement c in episodeNode.ChildNodes)
            {
                if (c.InnerText == "")
                    continue;
                int id;
                    switch (c.Name)
                    {
                        case "id":
                            if(Int32.TryParse(c.InnerText, out id))
                                s.ID = id;
                            break;
                        case "SeasonNumber":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.Season = id;
                            break;
                        case "EpisodeNumber":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.EpisodeNumber = id;
                            break;
                        case "DVD_chapter":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.DVDChapter = id;
                            break;
                        case "DVD_discid":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.DVDDiscID = id;
                            break;
                        case "DVD_episodenumber":
                            if (Int32.TryParse(c.InnerText, out id))


                                s.DVDEpisode = id;
                            
                            break;
                        case "DVD_season":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.DVDSeason = id;
                            break;
                        case "Director":
                            s.Director = c.InnerText;
                            break;
                        case "EpisodeName":
                            s.Name = c.InnerText;
                            break;
                        case "FirstAired":
                            DateTime dt;
                            if (DateTime.TryParse(c.InnerText, out dt))

                                s.FirstAired = dt;
                            break;
                        case "GuestStars":
                            s.GuestStars.AddRange(c.InnerText.Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries));

                            for (int i = 0; i < s.GuestStars.Count; i++)
                            {
                                s.GuestStars[i] = s.GuestStars[i].Trim();
                            }
                            break;

                        case "Writer":
                            s.Writers.AddRange(c.InnerText.Split(new char[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries));

                            for (int i = 0; i < s.Writers.Count; i++)
                            {
                                s.Writers[i] = s.Writers[i].Trim();
                            }
                            break;
                        case "ProductionCode":
                            s.ProductionCode = c.InnerText;
                            break;

                        case "IMDB_ID":
                            s.ImdbID = c.InnerText;
                            break;
                        case "Language":
                            s.Language = c.InnerText;
                            break;

                        case "filename":
                            s.FileName = c.InnerText;
                            break;
                        case "Overview":
                            s.Overview = c.InnerText;
                            break;
                        case "Rating":
                            float f;
                            if (Single.TryParse(c.InnerText, NumberStyles.Float, CultureInfo.GetCultureInfo("en-US"), out f))
                                s.Rating = f;
                            break;
                        case "seasonid":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.SeasonID = id;
                            break;
                        case "seriesid":
                            if (Int32.TryParse(c.InnerText, out id))
                                s.SeriesID = id;
                            break;

                        case "lastupdated":
                            long l;
                            if (Int64.TryParse(c.InnerText, out l))
                                s.LastUpdated = l;
                            break;

                    }
            }
            return s;
        }

    }
}
